package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.common.DeviceRealTimeInformation;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.DeviceBatchRealTimeRequestBody;
import com.iot.inter.entity.newpojo.request.RequestBody.DeviceSingleRealTimeRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.DeviceBatchRealTimeRequest;
import com.iot.inter.entity.newpojo.request.subRequest.DeviceSingleRealTimeRequest;
import com.iot.inter.entity.newpojo.response.subResponse.DeviceBatchRealTimeResponse;
import com.iot.inter.entity.newpojo.response.subResponse.DeviceSingleRealTimeResponse;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * 去北京做：查询拥有的设备信息:2.0接口
 *
 * @author lm
 * @date 2019/8/26
 */
public class TurnToDeviceService {
    private static Logger logger = Logger.getLogger(TurnToDeviceService.class);

    /**
     * 去北京做：3.3	查询单一设备实时信息:2.0接口
     * 接入方可以通过调用该接口，查询某个设备信息，包括设备的基本信息、设备当前订购的套餐信息等。
     *
     * @author lm
     * @date 2019/8/26
     */

    public DeviceRealTimeInformation singleDeviceRealTime(String deviceId, String secret, String partnerCode) {

        try {
            DeviceSingleRealTimeRequestBody body = new DeviceSingleRealTimeRequestBody();
            body.setDeviceId(deviceId);

            DeviceSingleRealTimeRequest request = new DeviceSingleRealTimeRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();

            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_singleDevice, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(DeviceSingleRealTimeRequest.class).toXml(request, "utf-8");
            logger.info("【查询单一设备实时信息】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_singleDevice);

            logger.info("【查询单一设备实时信息】xml响应：" + result);

            DeviceSingleRealTimeResponse responseObj = new JaxbUtil(DeviceSingleRealTimeResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();

            if (null != responseObj && true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {

                deviceId = responseObj.getBody().getDeviceId();
                String otaFlag = responseObj.getBody().getOtaFlag();
                String bipFlag = responseObj.getBody().getBipFlag();
                String multiImsiFlag = responseObj.getBody().getMultiImsiFlag();
                String lifecycle = responseObj.getBody().getLifecycle();
                String lifecycleStartTime = responseObj.getBody().getLifecycleStartTime();
                String lifecycleEndTime = responseObj.getBody().getLifecycleEndTime();
                String lifecycleSlientPeriod = responseObj.getBody().getLifecycleSlientPeriod();
                String lifecycleShutdownPeriod = responseObj.getBody().getLifecycleShutdownPeriod();

                String orderCode = responseObj.getBody().getPackageOrder().getOrderCode();
                String packageName = responseObj.getBody().getPackageOrder().getPackageName();
                String packageCode = responseObj.getBody().getPackageOrder().getPackageCode();
                String packageType = responseObj.getBody().getPackageOrder().getPackageType();
                String buyType = responseObj.getBody().getPackageOrder().getBuyType();
                String orderPeriod = responseObj.getBody().getPackageOrder().getOrderPeriod();
                String flow = responseObj.getBody().getPackageOrder().getFlow();
                String usedFlow = responseObj.getBody().getPackageOrder().getUsedFlow();
                String flowUseRate = responseObj.getBody().getPackageOrder().getFlowUseRate();
                String orderUsedFlow = responseObj.getBody().getPackageOrder().getOrderUsedFlow();
                String orderDate = responseObj.getBody().getPackageOrder().getOrderDate();
                String activeDate = responseObj.getBody().getPackageOrder().getActiveDate();
                String expireDate = responseObj.getBody().getPackageOrder().getExpireDate();
                String isOrderRefuelingPackage = responseObj.getBody().getPackageOrder().getIsOrderRefuelingPackage();

                if (isOrderRefuelingPackage.equals("1") && isOrderRefuelingPackage != null) {

                    String refOrderId = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefOrderId();
                    String refpackageStatus = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefpackageStatus();
                    String refPackageFlow = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefPackageFlow();
                    String refpackageOrderDate = responseObj.getBody().getPackageOrder().getRefuelingPackage().getRefpackageOrderDate();

                    DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                    information.setRetCode(retCode);
                    information.setResultInfo(resultInfo);
                    information.setDeviceId(deviceId);
                    information.setOtaFlag(otaFlag);
                    information.setBipFlag(bipFlag);
                    information.setMultiImsiFlag(multiImsiFlag);
                    information.setLifecycle(lifecycle);
                    information.setLifecycleStartTime(lifecycleStartTime);
                    information.setLifecycleEndTime(lifecycleEndTime);
                    information.setLifecycleSlientPeriod(lifecycleSlientPeriod);
                    information.setLifecycleShutdownPeriod(lifecycleShutdownPeriod);
                    information.setOrderCode(orderCode);
                    information.setPackageName(packageName);
                    information.setPackageCode(packageCode);
                    information.setPackageType(packageType);
                    information.setBuyType(buyType);
                    information.setOrderPeriod(orderPeriod);
                    information.setFlow(flow);
                    information.setUsedFlow(usedFlow);
                    information.setFlowUseRate(flowUseRate);
                    information.setOrderUsedFlow(orderUsedFlow);
                    information.setOrderDate(orderDate);
                    information.setActiveDate(activeDate);
                    information.setExpireDate(expireDate);
                    information.setIsOrderRefuelingPackage(isOrderRefuelingPackage);
                    information.setRefOrderId(refOrderId);
                    information.setRefpackageStatus(refpackageStatus);
                    information.setRefPackageFlow(refPackageFlow);
                    information.setRefpackageOrderDate(refpackageOrderDate);
                    return information;

                } else {
                    DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                    information.setRetCode(retCode);
                    information.setResultInfo(resultInfo);
                    information.setDeviceId(deviceId);
                    information.setOtaFlag(otaFlag);
                    information.setBipFlag(bipFlag);
                    information.setMultiImsiFlag(multiImsiFlag);
                    information.setLifecycle(lifecycle);
                    information.setLifecycleStartTime(lifecycleStartTime);
                    information.setLifecycleEndTime(lifecycleEndTime);
                    information.setLifecycleSlientPeriod(lifecycleSlientPeriod);
                    information.setLifecycleShutdownPeriod(lifecycleShutdownPeriod);
                    information.setOrderCode(orderCode);
                    information.setPackageName(packageName);
                    information.setPackageCode(packageCode);
                    information.setPackageType(packageType);
                    information.setBuyType(buyType);
                    information.setOrderPeriod(orderPeriod);
                    information.setFlow(flow);
                    information.setUsedFlow(usedFlow);
                    information.setFlowUseRate(flowUseRate);
                    information.setOrderUsedFlow(orderUsedFlow);
                    information.setOrderDate(orderDate);
                    information.setActiveDate(activeDate);
                    information.setExpireDate(expireDate);
                    information.setIsOrderRefuelingPackage(isOrderRefuelingPackage);
                    return information;

                }

            } else {
                //也要把结果信息返回回去
                DeviceRealTimeInformation information = new DeviceRealTimeInformation();
                information.setRetCode(retCode);
                information.setResultInfo(resultInfo);
                return information;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 去北京做：3.2	批量获取拥有的设备信息:2.0接口
     * 接入方可以通过调用该接口，批量查询其拥有的设备信息，包括设备的基本信息、设备当前订购的套餐信息等。
     * 考虑到拥有的设备数量会很多，所以本接口按照分页形式，每次查询仅获得一页设备信息，其中每页包含的设备数量，由接入方自行定义。
     * 若要获取所有设备信息，则需要多次调用该接口。
     *
     * @author lm
     * @date 2019/8/26
     */
    public DeviceBatchRealTimeResponse batchDeviceRealTime(Integer pageNo, Integer pageSize, String secret, String partnerCode) {

        try {
            DeviceBatchRealTimeRequestBody body = new DeviceBatchRealTimeRequestBody();

            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }

            DeviceBatchRealTimeRequest request = new DeviceBatchRealTimeRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();

            //拼装头信息
            RequestHeader header = utils.getRequestHeader(Constant.cmd_batchDevice, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(DeviceBatchRealTimeRequest.class).toXml(request, "utf-8");
            logger.info("【批量获取拥有的设备信息】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("url=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_batchDevice);

            logger.info("【批量获取拥有的设备信息】xml响应：" + result);

            DeviceBatchRealTimeResponse responseObj = new JaxbUtil(DeviceBatchRealTimeResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();


            //把拿到的回传结果直接给到接口,只有DeviceBatchRealTimeResponse才包括所有信息
            DeviceBatchRealTimeResponse re = new DeviceBatchRealTimeResponse();
            re.setBody(responseObj.getBody());
            re.setRetCode(retCode);
            re.setResultInfo(resultInfo);
            return re;


        } catch (Exception e) {

            DeviceBatchRealTimeResponse re = new DeviceBatchRealTimeResponse();
            re.setRetCode("9999");
            re.setResultInfo("接口出现异常");
            return re;
        }

    }

}

